// Copyright (c) 2011-2012 SameGoal LLC.
// All Rights Reserved.
// Author: Andy Hochhaus <ahochhaus@samegoal.com>

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef NET_HTTP_SERVER_COMET_COMET_H_
#define NET_HTTP_SERVER_COMET_COMET_H_

#include <fcntl.h>
#include <gflags/gflags.h>
#include <glog/logging.h>

#include <list>
#include <map>
#include <string>
#include <vector>

#include "base/basictypes.h"
#include "net/http/server/comet/request_helper.h"
#include "net/http/server/comet/timer_wrapper.h"
#include "net/http/server/event/event_server.h"
#include "net/http/server/event/handler.h"

namespace sg {
namespace net {
namespace http {
namespace server {
namespace comet {

namespace es = ::sg::net::http::server::event;

class CometServer : public event::EventServer {
 public:
  CometServer(const std::string &ip,
              const std::string &hostname,
              const std::string &port,
              const uint32 app_callback_pool_size);

  typedef void (*Callback)(es::Request *req, void *arg);
  typedef std::map<std::string, std::string> IncomingMap;
  typedef bool (*ChannelCallback)(const std::string &sid,
                                  const IncomingMap &map,
                                  es::Request *req);
  typedef void (*CreateSessionCallback)(const std::string &sid,
                                        es::Request *req);
  typedef void (*CloseSessionCallback)(const std::string &sid);

  void SetAccessControlAllowOrigin(const std::string &origin);
  void SetChannelHandler(ChannelCallback callback);
  void SetCreateSessionHandler(CreateSessionCallback callback);
  void SetCloseSessionHandler(CloseSessionCallback callback);
  void SetHostPrefix(const std::string &host_prefix);
  void SetBlockedPrefix(const std::string &blocked_prefix);

  bool Send(const std::string &sid, const std::string &message);
  void CloseSession(const std::string &sid);

  void InstallCometHandlers();

 private:
  static void TestHandler(es::Request *req, void *arg);
  static void BindHandler(es::Request *req, void *arg);
  static void CleardotHandler(es::Request *req, void *arg);
  static void RedirectHandler(es::Request *req, void *arg);
  static void GenericHandler(es::Request *req, void *arg);
  static void ConnectionCloseHandler(es::Request *req, void *arg);

  std::string host_prefix_, blocked_prefix_, origin_;
  es::Handler test_handler_, bind_handler_, cleardot_handler_,
      redirect_handler_;

  ChannelCallback channel_handler_;
  CreateSessionCallback create_session_handler_;
  CloseSessionCallback close_session_handler_;

  DISALLOW_COPY_AND_ASSIGN(CometServer);
};

}  // namespace comet
}  // namespace server
}  // namespace http
}  // namespace net
}  // namespace sg

#endif  // NET_HTTP_SERVER_COMET_COMET_H_
